package com.neulogistics.core.substation.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.core.substation.domain.model.aggregate.SubstationPayment;
import com.neulogistics.core.substation.domain.model.enumeration.PaymentType;
import com.neulogistics.core.substation.domain.model.valueobject.Address;
import com.neulogistics.core.substation.domain.model.valueobject.CourierInfo;
import com.neulogistics.core.substation.domain.model.valueobject.CustomInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderItem;
import com.neulogistics.core.substation.infrastructure.persistence.orm.po.SubstationPaymentPO;
import org.springframework.beans.BeanUtils;

public class SubstationPaymentEntityPOConverter {
    private static final int Rate = 100;

    public static SubstationPaymentPO entityToPO(SubstationPayment substationPayment){
        if(substationPayment == null)
            return null;

        SubstationPaymentPO substationPaymentPO = new SubstationPaymentPO();
        // 复制普通属性
        BeanUtils.copyProperties(substationPayment, substationPaymentPO);

        // 值对象转换
        if(substationPayment.getCourierInfo() != null){
            substationPaymentPO.setCourierInfo(JSONUtil.parseObj(substationPayment.getCourierInfo()));
        }
        if(substationPayment.getCustomInfo() != null){
            substationPaymentPO.setCustomInfo(JSONUtil.parseObj(substationPayment.getCustomInfo()));
        }
        if(substationPayment.getOrderItems() != null){
            substationPaymentPO.setOrderItems(JSONUtil.parseArray(substationPayment.getOrderItems()));
        }
        // 枚举值转换
        if(substationPayment.getPaymentType() != null){
            substationPaymentPO.setPaymentType(substationPayment.getPaymentType().id);
        }
        // 金额转换
        if(substationPayment.getPaymentTotal() != null){
            substationPaymentPO.setPaymentTotal((int) (substationPayment.getPaymentTotal() * Rate));
        }

        return substationPaymentPO;
    }

    public static SubstationPayment POToEntity(SubstationPaymentPO substationPaymentPO){
        if(substationPaymentPO == null)
            return null;

        SubstationPayment substationPayment = new SubstationPayment();
        // 复制普通属性, id是可以在这里被复制进去的
        BeanUtils.copyProperties(substationPaymentPO, substationPayment);
        // json对象转换为值对象
        substationPayment.setCourierInfo(JSONUtil.toBean(substationPaymentPO.getCourierInfo(), CourierInfo.class));
        substationPayment.setCustomInfo(JSONUtil.toBean(substationPaymentPO.getCustomInfo(), CustomInfo.class));
        substationPayment.setOrderItems(JSONUtil.toList(substationPaymentPO.getOrderItems(), OrderItem.class));
        // 枚举值转换
        substationPayment.setPaymentType(PaymentType.fromValue(substationPaymentPO.getPaymentType(), PaymentType.class));
        // 金额转换
        substationPayment.setPaymentTotal(Double.valueOf(substationPaymentPO.getPaymentTotal()) / Rate);

        return substationPayment;
    }

}
